package com.component.slider
{
	import com.component.ComponentChildren;
	import com.component.button.BitmapDataButtonSkin;
	import com.component.button.Button;
	import com.component.button.GraphButtonSkin;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	public class ScaleSlider extends ComponentChildren
	{
		public var btnBar:Button;
		public var start:int;
		public var rect:Rectangle;
		public function ScaleSlider(width:uint, height:uint)
		{
			super();
			_width = width;
			_height = height;
			btnBar = new Button("", 7, 17, new GraphButtonSkin());
			
			btnBar.x = start = Math.floor((width - 7) / 2);
			btnBar.y = Math.floor((height - 17) / 2);
			addChild(btnBar);
			
			graphics.beginFill(0xdadada, .5);
			graphics.drawRect(0, 0, width - 3, height);
			graphics.endFill();
			rect = new Rectangle(-3, btnBar.y, width, 0);
			btnBar.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
		}
		private function mouseDownHandler(e:MouseEvent):void{
			btnBar.startDrag(false, rect);
			btnBar.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			btnBar.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		}
		private function mouseMoveHandler(e:MouseEvent):void{
			dispatchEvent(new Event(Event.CHANGE));
		}
		private function mouseUpHandler(e:MouseEvent):void{
			btnBar.stopDrag();
			btnBar.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			btnBar.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		public function get current():Number{
			return (btnBar.x + 3) / width;
		}
		public function set current(value:Number):void{
			btnBar.x = value * width - 3;
			dispatchEvent(new Event(Event.CHANGE));
		}
	}
}